﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Packets;
using WorldServer.Models;
using WorldServer.Services;
using AionCxxLibrary.Models;
using WorldServer.DAO;
using AionCxxLibrary.Utilities;
using AionCxxLibrary.Enums;

namespace WorldServer.Packets.Server
{
    public class SPkt_SocialFriends : AionServerPacket
    {
        public override void WritePacket()
        {
            Character thisChar = ((WorldConnection)Connection).Character;
            FriendsList list = thisChar.Friends;

            Dictionary<TCharacter, bool> Friends = new Dictionary<TCharacter, bool>();

            foreach (int friendId in list)
            {
                TCharacter template = null;
                Character c = LevelService.GetCharacter(friendId);
                if (c != null)
                    template = c.Template;
                else
                {
                    template = CharacterDAO.SelectCharacterTemplate(friendId);
                }

                if (template == null)
                {
                    FriendsDAO.RemoveFriend(thisChar.GetGUID(), friendId);
                }
                else
                {
                    Friends.Add(template, c != null);
                }
            }

            WriteShort((short)(0 - Friends.Count));
            WriteByte(0);

            foreach (KeyValuePair<TCharacter, bool> friend in Friends)
            {
                WriteString(friend.Key.Name);
                WriteInt(friend.Key.Level);
                WriteInt((int)friend.Key.Class);
                WriteByte(1);
                WriteInt(friend.Key.WorldID);
                WriteInt((int)Utility.DateTimeToTimestamp(DateTime.Now));
                WriteString(friend.Key.Note);
                if (friend.Value)
                {
                    Character c = LevelService.GetCharacter(friend.Key.ID);
                    WriteByte(c.SocialState);
                }
                else
                    WriteByte(0); // Offline user
            }
        }
    }

    public class SPkt_SocialBlocks : AionServerPacket
    {
        public override void WritePacket()
        {
            Character Char = ((WorldConnection)Connection).Character;            

            WriteShort((short)Char.Blocks.List.Count);
            WriteByte(0);

            foreach (KeyValuePair<string,string> block in Char.Blocks.List)
            {
                WriteString(block.Key);
                WriteString(block.Value);
            }
        }
    }

    public class SPkt_SocialPublicMessage : AionServerPacket
    {
        PublicMessageType type;
        Character sender;
        string message;
        public SPkt_SocialPublicMessage(PublicMessageType type, Character sender, string message)
        {
            this.type = type;
            this.sender = sender;
            this.message = message;
        }

        public override void WritePacket()
        {
            WriteByte((byte)(int)type);

            bool canRead = type == PublicMessageType.SystemAlert || type == PublicMessageType.SystemWhite || type == PublicMessageType.SystemYellow
                || (sender.Template.Race == (Connection as WorldConnection).Character.Template.Race);
            byte flag = canRead ? (byte)0 : (byte)((int)sender.Template.Race + 1);
            WriteByte(flag);
            WriteInt(sender != null ? sender.GetGUID() : 0);
            switch (type)
            {
                case PublicMessageType.Regular:
                case PublicMessageType.SystemAlert:
                case PublicMessageType.SystemWhite:
                case PublicMessageType.SystemYellow:
                    WriteShort(0);
                    WriteString(message);
                    break;
                case PublicMessageType.Shout:
                    WriteString(sender.Template.Name);
                    WriteString(message);
                    WriteFloat(sender.Position.x);
                    WriteFloat(sender.Position.y);
                    WriteFloat(sender.Position.z);
                    break;
                case PublicMessageType.Alliance:
                case PublicMessageType.Group:
                case PublicMessageType.GroupLeader:
                case PublicMessageType.Legion:
                case PublicMessageType.Whisper:
                    WriteString(sender.Template.Name);
                    WriteString(message);
                    break;
            }
        }
    }

    public class SPkt_SystemMessage : AionServerPacket
    {
        bool PeriodMsg;
        int MsgID;
        int ObjectID;
        object[] Params;
        public SPkt_SystemMessage(bool NPCPeriodicMessage, int messageId, int objectId, params object[] parameters)
        {
            PeriodMsg = NPCPeriodicMessage;
            MsgID = messageId;
            Params = parameters;
            ObjectID = objectId;
        }
        public override void WritePacket()
        {
            WriteShort(PeriodMsg ? (short)1 : (short)0);
            WriteInt(ObjectID);
            WriteInt(MsgID);
            WriteByte(Params != null ? (byte)Params.Length : (byte)0);
            if (Params != null)
            {
                foreach(object p in Params)
                {
                    if (p is AionDescription)
                    {
                        WriteShort(0x24);
                        WriteInt((p as AionDescription).DescriptionID);
                        WriteShort(0);
                    }
                    else
                        WriteString(p.ToString());
                }
            }
            WriteShort(PeriodMsg ? (byte)1 : (byte)0);
        }
    }

    public class SPkt_ChatServerKey : AionServerPacket
    {
        byte[] key;
        public SPkt_ChatServerKey(byte[] key)
        {
            this.key = key;
        }

        public override void WritePacket()
        {
            WriteInt(key.Length);
            WriteBytes(key);
        }

    }

    public class SPkt_Emotion : AionServerPacket
    {
        int objectId;
        EmotionType etype;
        float x = 0, y = 0, z = 0;
        byte heading;
        int targetId;
        ushort emotionId;

        public SPkt_Emotion(int objectId, EmotionType type)
        {
            this.objectId = objectId;
            this.etype = type;
        }

        public SPkt_Emotion(int objectId, EmotionType type, float x, float y, float z, byte heading, int targetId, ushort emotionId)
        {
            this.objectId = objectId;
            this.etype = type;
            this.x = x;
            this.y = y;
            this.z = z;
            this.heading = heading;
            this.targetId = targetId;
            this.emotionId = emotionId;
        }

        public override void WritePacket()
        {
            WriteInt(objectId);
            WriteByte((byte)etype);
            switch (etype)
            {
                case EmotionType.SelectTarget:
                case EmotionType.Jump:
                case EmotionType.Sit:
                case EmotionType.Stand:
                case EmotionType.Airline_Stop:
                case EmotionType.Fly:
                case EmotionType.Land:
                case EmotionType.Resurrect:
                case EmotionType.AttackMode:
                case EmotionType.AttackMode2:
                case EmotionType.RegularMode:
                case EmotionType.Walk:
                case EmotionType.Run:
                case EmotionType.PrivateShop_Start:
                case EmotionType.PrivateShop_Stop:
                case EmotionType.Powershard_Enable:
                case EmotionType.Powershard_Disable:
                    WriteShort(1); // State
                    WriteFloat(6f); // Speed
                    break;
                case EmotionType.SwitchDoor:
                    WriteShort(9);
                    WriteFloat(6f);
                    break;
                case EmotionType.Loot_End:
                case EmotionType.Loot_Start:
                case EmotionType.QuestLoot_End:
                case EmotionType.QuestLoot_Start:
                case EmotionType.ToyPetFeed:
                case EmotionType.ToyPetFeed2:
                    WriteShort(1);
                    WriteFloat(6f);
                    WriteInt(targetId);
                    break;
                case EmotionType.Emote_Start:
                case EmotionType.Emote_Start2:
                    WriteShort(1);
                    WriteFloat(6f);
                    WriteUnsignedShort(6000);
                    WriteUnsignedShort(6000);
                    break;
                case EmotionType.Airline_Start:
                    WriteShort(1);
                    WriteFloat(6f);
                    WriteInt(0);
                    break;
                case EmotionType.Windstream_Start:
                    WriteShort(2);
                    WriteFloat(7.32f);
                    WriteInt(0);
                    WriteInt(targetId);
                    break;
                case EmotionType.Windstream_Boost:
                    WriteShort(2);
                    WriteFloat(7.32f);
                    break;
                case EmotionType.Windstream_Stop:
                    WriteByte(1);
                    WriteByte(2);
                    WriteFloat(7.32f);
                    break;
                case EmotionType.Chair_Sit:
                case EmotionType.Chair_Up:
                    WriteShort(1);
                    WriteFloat(6f);
                    WriteFloat(x);
                    WriteFloat(y);
                    WriteFloat(z);
                    WriteByte(heading);
                    break;
                case EmotionType.Death:
                    WriteShort(1);
                    WriteFloat(6f);
                    WriteInt(targetId);
                    break;
                case EmotionType.SocialEmote:
                    WriteShort(1);
                    WriteFloat(6f);
                    WriteInt(targetId);
                    WriteUnsignedShort(emotionId);
                    WriteByte(1);
                    break;
                default:
                    WriteShort(1);
                    WriteFloat(6f);
                    if (targetId > 0)
                        WriteInt(targetId);
                    break;
            }
        }
    }

}
